//创建用户的小仓库
import {defineStore} from "pinia";
//引入登录请求
import {reqLogin, reqUserInfo, reqLogout} from "../../api/user/userApi.ts";
//引入常量路由
//@ts-ignore
import {constantRoutes, asyncRoute, anyRoute} from "../../router/constantRoutes.ts";
//引入ts类型
import type {T_loginFormData, T_loginResponseData, T_userInfoResponseData} from "../../api/user/type.ts";
//引入路由器对象
//@ts-ignore
import router from "../../router/routerConfig.ts";
//引入深拷贝方法
//@ts-ignore
import cloneDeep from 'lodash/cloneDeep';


//用于过滤当前用户需要展示的异步路由
const filterAsyncRoute = (asyncRoute: any, routes: any) => {
    return asyncRoute.filter((item: any) => {
        if (routes.includes(item.name)) {
            if (item.children && item.children.length > 0) {
                item.children = filterAsyncRoute(item.children, routes);
            }
            return true;
        }
    })
}

let userStore = defineStore('User', {
    //小仓库
    state: (): any => {//函数返回的类型是UserState类型
        return {
            token: localStorage.getItem("TOKEN"),//用户的唯一标识
            menuRoutes: constantRoutes, //仓库存储生成菜单需要的数组
            username: '',
            avatar: '',
        }
    },
    //异步|逻辑的地方
    actions: {
        //用户登录的方法
        async userLogin(data: T_loginFormData) {
            let result: T_loginResponseData = await reqLogin(data);
            //登录成功：200--> token
            //登录请求：201-->错误信息
            if (result.code == 200) {
                this.token = (result.data as string)
                //本地化持久化存储一份
                localStorage.setItem("TOKEN", (result.data as string))
                //返回一个成功的promise
                return 'ok';
            } else {
                //登录失败
                return Promise.reject(new Error(result.data));

            }
        },

        //获取用户信息方法
        async userInfo() {
            //获取用户信息 进行存储仓库当中[用户头像、名字]
            let result: T_userInfoResponseData = await reqUserInfo();

            //如果成功获取用户信息，存储用户信息
            if (result.code == 200) {
                this.username = result.data.name
                this.avatar = result.data.avatar;
                //计算当前用户需要展示的路由
                //let userAsyncRoute: any = filterAsyncRoute(cloneDeep(asyncRoute), result.data.routes);
                //菜单的数据
                //this.menuRoutes = [...constantRoutes, ...asyncRoute, anyRoute];
                //目前路由器管理的只有常量路由
                //将异步路由注册到路由器对象
                //[...asyncRoute, anyRoute].forEach((route: any) => router.addRoute(route))
                return 'ok';
            } else {
                return Promise.reject(new Error(result.message));
            }
        },

        //用户登出方法
        async userLogOut() {
            let result: any = await reqLogout();
            if (result.code == 200) {
                //退出登录成功
                this.token = '';
                this.username = '';
                this.avatar = '';
                localStorage.removeItem("TOKEN");
                return 'ok';//返回一个成功的promise 让组件知道退出成功 然后进行跳转
            } else {
                //退出登录失败
                return Promise.reject(new Error(result.message))
            }

        },


    },
    getters: {}
})
//对外暴露
export default userStore;